﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iBuproModel;
using iBuproModel.Repositories;
using iBuproModel.Repositories.Classes;
using iBuproModel.Repositories.Interfaces;
using IBupro.Models;
using IBupro.Controllers.Classes;

namespace IBupro.Controllers
{
    /// <summary>
    /// This controller handles Student field (CZ - studinjní obory) business logic
    /// </summary>
    public class StudentFieldController : CustomController
    {


        IStudentFieldRepository repository;

        public StudentFieldController()
        {
            this.repository = repositoryService.StudentFieldRepositoryCreateInstance();
        }

        public StudentFieldController(IStudentFieldRepository repository)
        {
            this.repository = repository;
        }

        [Authorize(Roles="Admin")]
        public ActionResult Index()
        {
            return View(repository.GetAll());
        }

        //
        // GET: /StudentField/Details/5
         [Authorize(Roles = "Admin")]
        public ActionResult Details(int id)
        {
            return View(repository.First(f => f.Id == id));
        }

        //
        // GET: /StudentField/Create
         [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View(new StudentFieldModel());
        } 

        //
        // POST: /StudentField/Create
         [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Create(StudentFieldModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    StudentField newField = new StudentField();
                    newField.Name = model.Name;
                    newField.Shortcut = model.Shortcut;

                    if (model.SelectedSubjects != null)
                        repository.UpdateSubjectList(newField,model.SelectedSubjects);

                    IStudentPlanRepository studentPlans = repositoryService.StudentPlanRepositoryCreateInstance();
                    repository.AddStudentFieldToStudentPlan(newField, studentPlans.First(p => p.Id == model.StudentPlan));
                    

                    repository.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

         [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            StudentField storedStudentField = repository.First(f => f.Id == id);
            StudentFieldModel model = new StudentFieldModel(storedStudentField.Name, storedStudentField.Shortcut, storedStudentField.StudentPlan.Id);
            
            List<string> selected = new List<string>();

            foreach (Subject subject in storedStudentField.Subjects)
            {
                selected.Add(subject.Id.ToString());
            }

            ViewData["selected"] = selected.ToArray();

            return View(new StudentFieldModel(storedStudentField.Name,storedStudentField.Shortcut,storedStudentField.StudentPlan.Id));
        }
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Edit(int id, StudentFieldModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    StudentField newField = repository.First(f => f.Id == id);
                    newField.Name = model.Name;
                    newField.Shortcut = model.Shortcut;

                    if (model.SelectedSubjects != null)
                        repository.UpdateSubjectList(newField, model.SelectedSubjects);



                    IStudentPlanRepository studentPlans = repositoryService.StudentPlanRepositoryCreateInstance();
                    newField.StudentPlan = studentPlans.First(p => p.Id == model.StudentPlan);


                    repository.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /StudentField/Delete/5
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
                repository.Hide(id);
                repository.SaveChanges();

                return RedirectToAction("Index");
            
            
        }

       
    }
}
